from . import web
from flask import request
from .GitlabMessage import Message,LineTextItem,KeyValueItem,SubMsgItem,EmptyItem
from app import dingding
import time

token = '99a07a3659518b137b46dfcbb7e29eb42d55d074fb90b8f7b53e058ea38c880f'


@web.route('/trigger' , methods =['POST'])
def trigger():
    data = request.get_json()
    print(data)

    # push
    event_name = data['event_name']
    if 'push' == event_name:
        title, text = get_push(data)
        mdmsg = dingding.MarkdownMessage()
        mdmsg.setTitle(title)
        mdmsg.setText(text)

        print(text)
        dingding.sendMsg(token , mdmsg.to_json_string())

    return "success"


def get_push(dic):
    message = Message()

    message.set_title('有新的项目推送')

    # gitlab创建者，可称项目管理员
    user_name = dic.get('user_name')
    # 总推送的次数
    total_commits_count = dic.get('total_commits_count')

    project_dic = dic.get('project')

    # 项目名称
    project_name = project_dic.get('name')
    # 项目描述
    project_desc = project_dic.get('description')
    # 项目地址,url
    project_url = project_dic.get('web_url')
    # 项目分支
    project_branch = project_dic.get('default_branch')

    message.add_item(KeyValueItem('项目名称' , project_name))
    message.add_item(KeyValueItem('项目描述' , project_desc))
    message.add_item(KeyValueItem('项目分支' , project_branch))
    message.add_item(KeyValueItem('项目地址' , '[' + project_name + '](' + project_url + ')'))
    message.add_item(KeyValueItem('项目管理员' , user_name))
    message.add_item(KeyValueItem('总推送次数' , str(total_commits_count)))
    message.add_item(KeyValueItem('推送时间' , time.strftime('%Y-%m-%d %H:%M:%S' , time.localtime())))

    message.add_item(EmptyItem())

    commit_list = dic.get('commits')

    for it in commit_list:
        # 提交时写的注释
        commit_message = it.get('message')
        # 此次提交信息的查看url
        commit_info_url = it.get('url')
        # 增加的文件列表
        add_file_list = it.get('added')
        # 修改的文件列表
        modify_file_list = it.get('modified')
        # 删除的文件列表
        delete_file_list = it.get('removed')
        # commit的时间
        commit_time = time.strftime('%Y-%m-%d %H-%M-%S' , time.strptime(it.get('timestamp') , '%Y-%m-%dT%H:%M:%SZ'))

        author_dic = it.get('author')

        # 提交者name
        author_username = author_dic.get('name')
        # 提交者email
        author_email = author_dic.get('email')

        message.add_item(LineTextItem('## 代码提交列表' , False))

        sub_item = SubMsgItem()
        sub_item.set_dot(False)
        sub_item.add_item(KeyValueItem('提交人' , author_username , False))
        sub_item.add_item(KeyValueItem('提交人email' , author_email , False))
        sub_item.add_item(KeyValueItem('提交信息' , commit_message , False))
        sub_item.add_item(KeyValueItem('提交详情' , '[查看详情](' + commit_info_url + ')' , False))
        sub_item.add_item(KeyValueItem('增加文件' , len(add_file_list) , False))
        sub_item.add_item(KeyValueItem('修改文件' , len(modify_file_list) , False))
        sub_item.add_item(KeyValueItem('删除文件' , len(delete_file_list) , False))
        sub_item.add_item(KeyValueItem('提交时间' , commit_time , False))

        message.add_item(sub_item)
        message.add_item(EmptyItem())


    return '有新的项目推送' , message.toMarkdownMessage()